<script>
  ;(function() {
    var config = window.whistleMenuConfig;
    var list = [];
    var networkListeners = [];
    var rulesListeners = [];
    var valuesListeners = [];
    var handleOnLoad = function() {
      timer = null;
      list.forEach(emit);
      list = null;
    };
    var timer = setTimeout(handleOnLoad, 12000);

    function execListeners(listeners, options) {
      listeners.forEach(function(fn) {
        try {
          fn(options)
        } catch (e) {
          console.error(e);
        }
      });
    }

    function emit(options) {
      switch(options.type) {
        case 'rules':
          execListeners(rulesListeners, options);
          break;
        case 'values':
          execListeners(valuesListeners, options);
          break;
        default:
          execListeners(networkListeners, options);
      }
    }
    window.onWhistleContextMenuClick = function(options) {
      if (list) {
        list.push(options);
      } else {
        emit(options);
      }
    };
    var delayOnLoad = function() {
      if (timer) {
        setTimeout(handleOnLoad, 100);
        clearTimeout(timer);
        timer = null;
      }
    };
    window.onload = delayOnLoad;
    try {
      window.addEventListener('load', delayOnLoad);
    } catch (e) {}
    var toast = {};
    var modal = {};
    window.whistleBridge = {
      config: config,
      modal: modal,
      toast: toast,
      addNetworkListener: function(l) {
        if (typeof l === 'function' && networkListeners.indexOf(l) === -1) {
          networkListeners.push(l);
        }
      },
      removeNetworkListener: function(l) {
        l = networkListeners.indexOf(l);
        if (l !== -1) {
          networkListeners.splice(l, 1);
        }
      },
      removeAllNetworkListeners: function() {
        networkListeners = [];
      },
      addRulesListener: function(l) {
        if (typeof l === 'function' && rulesListeners.indexOf(l) === -1) {
          rulesListeners.push(l);
        }
      },
      removeRulesListener: function(l) {
        l = rulesListeners.indexOf(l);
        if (l !== -1) {
          rulesListeners.splice(l, 1);
        }
      },
      removeAllRulesListeners: function() {
        rulesListeners = [];
      },
      addValuesListener: function(l) {
        if (typeof l === 'function' && valuesListeners.indexOf(l) === -1) {
          valuesListeners.push(l);
        }
      },
      removeValuesListener: function(l) {
        l = valuesListeners.indexOf(l);
        if (l !== -1) {
          valuesListeners.splice(l, 1);
        }
      },
      removeAllValuesListeners: function() {
        valuesListeners = [];
      }
    };
    try {
      window.initWhistleBridge = function(options) {
        window.initWhistleBridge = function() {};
        Object.keys(options.msgBox).forEach(function(name) {
          toast[name] = options.msgBox[name];
        });
        window.whistleBridge.importSessions = options.importSessions;
        window.whistleBridge.request = options.request;
        window.whistleBridge.createRequest = options.createRequest;
        window.whistleBridge.showModal = options.showModal;
        window.whistleBridge.createModal = options.createModal;
        window.whistleBridge.importRules = options.importRules;
        window.whistleBridge.importValues = options.importValues;
      };
      window.parent.onPluginContextMenuReady(window);
    } catch (e) {}
  })();
</script>
